//
//  LocationSearch.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/11/30.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "LocationSearchViewModel.h"
#import "AppConfig.h"
#import "NSUserDefaults+Addition.h"
@implementation LocationSearchViewModel

-(id)init{
    [self initData];
    return [super init];
}

-(void)initData{
    self.pois   =   [[NSMutableArray alloc ] init];
    self.searchPois=[[NSMutableArray alloc ] init];
    self.search  = [[AMapSearchAPI alloc] initWithSearchKey:[MAMapServices sharedServices].apiKey Delegate:self];
    self.request = [[AMapPlaceSearchRequest alloc ] init];
    /* 按照距离排序. */
    self.request.sortrule            = 1;
    self.request.requireExtension    = YES;
    
    
    self.searchPageCount = 30;
    self.poiPageCount    = 10;
    
}

/**
 *  poi热点搜索
 *
 *  @param cllocation  中心经纬度
 *  @param reslutBlock 回调
 */
-(void)requestPoi:(CLLocationCoordinate2D)cllocation reslutBlock:(DealDataResultBlock)reslutBlock{
    
    self.requestPoiBlock    = reslutBlock;
    self.loadMoreCLLocation = cllocation;
    self.request.searchType = AMapSearchType_PlaceAround;
    self.request.location   = [AMapGeoPoint locationWithLatitude:cllocation.latitude longitude:cllocation.longitude];
    self.request.page       = self.pageIndex;
    self.request.offset     = self.poiPageCount+1;
    
    if (self.pageIndex == 1) {
        [self.pois removeAllObjects];
        
        if (self.currentPoisModel) {
            [self.pois insertObject:self.currentPoisModel atIndex:0];
        }
    }
    
    [self.search AMapPlaceSearch:self.request];
}

/* poi热点搜索加载更多 */
-(void)loadMorePoiSearchReslutBlock:(DealDataResultBlock)reslutBlock{
    [self requestPoi:self.loadMoreCLLocation reslutBlock:reslutBlock];
}

/**
 *  关键字搜索
 *
 *  @param reslutBlock 回调
 */
-(void)requestTipReslutBlock:(DealDataResultBlock)reslutBlock{

    self.requestTipBlock    = reslutBlock;
    if (self.searchPageIndex == 1) {
        [self.searchPois removeAllObjects];
    }
    
    if ([self.requestTip isEqualToString:@""]) {
        
        if (self.requestTipBlock) {
            self.requestTipBlock(1,nil,nil);
        }
        
        return;
    }
    
    self.request.keywords   = self.requestTip;
    self.request.searchType = AMapSearchType_PlaceKeyword;
    self.request.location   = nil;
    self.request.offset     = self.searchPageCount + 1;
    NSString *city = [[NSUserDefaults standardUserDefaults] getUserSelectedCity];
    if (city) {
        self.request.city = @[city];
    }
   
    self.request.page = self.searchPageIndex;
    [self.search AMapPlaceSearch:self.request];
}

/* 关键字搜索加载更多 */
-(void)loadMoreTipSearchReslutBlock:(DealDataResultBlock)reslutBlock{
    [self requestTipReslutBlock:reslutBlock];
}

/* 搜索回调. */
- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)respons{
    if (self.isSearch) {
        
        if (respons.pois.count >= self.searchPageCount) {
            self.searchBMore = YES;
            self.searchPageIndex = self.searchPageIndex++;
        }
        WEAKSELF
        for (NSInteger i = 0; i < respons.pois.count; i ++) {
            AMapPOI *obj = respons.pois[i];
            
            //        if (![obj.address isEmptyOrWhitespace] && ![obj.name isEmptyOrWhitespace]) {
            PoisModel *poisModel = [[PoisModel alloc ] init];
            poisModel.name = obj.name;
            poisModel.address=obj.address;
            poisModel.latitude = obj.location.latitude;
            poisModel.longitude = obj.location.longitude;
            poisModel.city   = obj.city;
            [weakSelf.searchPois addObject:poisModel];
            //        }
        }
        if (self.requestTipBlock) {
            self.requestTipBlock(1,nil,nil);
        }
        
    }else{
        if (respons.pois.count >= self.poiPageCount) {
            self.bMore = YES;
            self.pageIndex = self.pageIndex++;
        }
        WEAKSELF
        for (NSInteger i = 0; i < respons.pois.count; i ++) {
            AMapPOI *obj = respons.pois[i];
            
            //        if (![obj.address isEmptyOrWhitespace] && ![obj.name isEmptyOrWhitespace]) {
            PoisModel *poisModel = [[PoisModel alloc ] init];
            poisModel.name = obj.name;
            poisModel.address=obj.address;
            poisModel.latitude = obj.location.latitude;
            poisModel.longitude = obj.location.longitude;
            poisModel.city   = obj.city;
            [weakSelf.pois addObject:poisModel];
            //        }
        }
        if (self.requestPoiBlock) {
            self.requestPoiBlock(1,nil,nil);
        }
   
    }
}

/**
 *  逆地理编码
 *
 *  @param cllocation  经纬度
 *  @param reslutBlock 回调
 */
-(void)requestGoecodeSearch:(CLLocationCoordinate2D)cllocation reslutBlock:(DealDataResultBlock)reslutBlock{
    
    self.goecodeBlock = reslutBlock;
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    regeo.location = [AMapGeoPoint locationWithLatitude:cllocation.latitude longitude:cllocation.longitude];
    regeo.requireExtension = YES;
    [self.search AMapReGoecodeSearch:regeo];
}

#pragma mark - AMapSearchDelegate
/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{
    if (response.regeocode != nil){
        self.currentPoisModel = [[PoisModel alloc ] init];
        
        AMapPOI *poi = [response.regeocode.pois firstObject];

        
        if (poi.name) {
            self.currentPoisModel.name       = [NSString stringWithFormat:@"[我的位置]%@",poi.name];

        }else{
            self.currentPoisModel.name       = @"[我的位置]";

        }
       
        self.currentPoisModel.address    = poi.address;
        self.currentPoisModel.latitude   = poi.location.latitude;
        self.currentPoisModel.longitude  = poi.location.longitude;
        self.currentPoisModel.city       = response.regeocode.addressComponent.city;
        [self.pois insertObject:self.currentPoisModel atIndex:0];
        
        if (self.goecodeBlock) {
            self.goecodeBlock(1,nil,nil);
        }
    
    }
}

@end
